package com.lulin.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.lulin.common.exception.BizCodEnum;
import com.lulin.mall.member.dao.MemberLevelDao;
import com.lulin.mall.member.exception.PhoneExistException;
import com.lulin.mall.member.exception.UsernameExistException;
import com.lulin.mall.member.vo.MemberLoginVo;
import com.lulin.mall.member.vo.MemberRegisterVo;
import com.lulin.mall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lulin.mall.member.entity.MemberEntity;
import com.lulin.mall.member.service.MemberService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;


/**
 * 会员
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 17:16:01
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 会员注册
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (UsernameExistException exception) {
            return R.error(BizCodEnum.USERNAME_UP_EXCEPTION.getCode(), BizCodEnum.USERNAME_UP_EXCEPTION.getMsg());
        } catch (PhoneExistException xistException) {
            return R.error(BizCodEnum.PHONE_UP_EXCEPTION.getCode(), BizCodEnum.PHONE_UP_EXCEPTION.getMsg());
        } catch (Exception e) {
            return R.error(BizCodEnum.UNKNOW_EXCEPTION.getCode(), BizCodEnum.UNKNOW_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 账号登录
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {
        //登录
        MemberEntity entity = memberService.login(vo);
        if (entity != null) {
            return R.ok().put("entity",JSON.toJSONString(entity));
        }
        //提示：账号或密码错误
        return R.error(BizCodEnum.USERNAME_PHONE_VALID_EXCEPTION.getCode(), BizCodEnum.USERNAME_PHONE_VALID_EXCEPTION.getMsg());
    }

    /**
     * 微博社交登录
     */
    @PostMapping("/oauth2/login")
    public R weiboSocialLogin(@RequestBody SocialUser vo) {
        //登录
        MemberEntity entity = memberService.weiboSocialLogin(vo);

        return R.ok().put("entity", JSON.toJSONString(entity));
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
